#include "vector3d.h"

Vector3D::Vector3D() {
	x = y = z = 0;
}
Vector3D::Vector3D(float a, float b) {
	x = a;
	y = b;
	z = 0;
}

Vector3D::Vector3D(float a, float b, float c) {
	x = a;
	y = b;
	z = c;
}

float Vector3D::dotProduct(const Vector3D& vec2) {
	return (x * vec2.x + y * vec2.y + z * vec2.z);
}

Vector3D Vector3D::crossProduct(const Vector3D& vec2) {
	return (Vector3D(y * vec2.z - z * vec2.y, x * vec2.z - z * vec2.x,
			x * vec2.y - y * vec2.x));
}

float Vector3D::length() {
	return sqrt(x * x + y * y + z * z);
}

void Vector3D::normalize() {
	float len = length();
	if (len != 0) {
		x /= len;
		y /= len;
		z /= len;
	}
}

void Vector3D::change(float a, float b, float c) {
	x = a;
	y = b;
	z = c;
}

void Vector3D::change(Vector3D vec2) {
	x = vec2.x;
	y = vec2.y;
	z = vec2.z;
}

void Vector3D::changeX(float a) {
	x = a;
}
void Vector3D::changeY(float a) {
	y = a;
}
void Vector3D::changeZ(float a) {
	z = a;
}

Vector3D Vector3D::operator+(const Vector3D& vec2) {
	return Vector3D(x + vec2.x, y + vec2.y, z + vec2.z);
}

Vector3D Vector3D::operator-(const Vector3D& vec2) {
	return Vector3D(x - vec2.x, y - vec2.y, z - vec2.z);
}

Vector3D Vector3D::operator*(float num) {
	return Vector3D(x * num, y * num, z * num);
}

Vector3D Vector3D::operator/(float num) {
	if (num != 0)
		return Vector3D(x / num, y / num, z / num);
	else
		return Vector3D();
}

Vector3D& Vector3D::operator+=(const Vector3D& vec2) {
	x += vec2.x;
	y += vec2.y;
	z += vec2.z;
	return *this;
}

Vector3D& Vector3D::operator-=(const Vector3D& vec2) {
	x -= vec2.x;
	y -= vec2.y;
	z -= vec2.z;
	return *this;
}

Vector3D& Vector3D::operator*=(float num) {
	x *= num;
	y *= num;
	z *= num;
	return *this;
}

Vector3D& Vector3D::operator/=(float num) {
	if (num != 0) {
		x /= num;
		y /= num;
		z /= num;
	}
	return *this;
}

bool Vector3D::operator==(const Vector3D vec2) {
	return (x == vec2.x && y == vec2.y && z == vec2.z);
}

bool Vector3D::operator!=(const Vector3D vec2) {
	return !(*this == vec2);
}

std::ostream& operator<<(std::ostream& out, const Vector3D& vec) {
	out << vec.x << " " << vec.y << " " << vec.z << std::endl;
	return out;
}

